#!/bin/bash
yum -y install  tftp-server dhcp-server syslinux-nonlinux net-tools

IP=`hostname -I |awk '{print $1}'`
SUBNET=`route |grep -E "ens|eth" |awk 'END {print $1}'`
NETMASK=`route |grep -E "ens|eth" |awk 'END {print $3}'`
RANGE1=${IP%.*}".10"
RANGE2=${IP%.*}".240"
ROUTERS=`ip route show | awk 'NR==1 {print $3}'`

cat <<EOF > /etc/dhcp/dhcpd.conf
subnet $SUBNET netmask $NETMASK {
range $RANGE1 $RANGE2;
option routers $ROUTERS;
option domain-name-servers 223.5.5.5;
next-server $IP;
filename "pxelinux.0";
}
EOF

cd  /var/lib/tftpboot/
curl -O "https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/isolinux/{initrd.img,ldlinux.c32,libcom32.c32,libutil.c32,vesamenu.c32,vmlinuz}"

cp /usr/share/syslinux/pxelinux.0 .

mkdir pxelinux.cfg
cd pxelinux.cfg

cat <<EOF > default
default vesamenu.c32
timeout 600
menu title PXE Auto Install Centos

label manual
  menu default
  menu label ^Manual Install Centos 8
  kernel vmlinuz
  append initrd=initrd.img inst.repo=https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/

label local
  menu label Boot from ^local drive
  localboot 0xffff

EOF

systemctl disable --now firewalld
systemctl enable --now tftp dhcpd
